// pages/review/review.js
import { repeat, getVideoDetail, getCommentList, addComment, VideoWatch } from "../../utils/api.js";
import { tsFormatTime, retTimeInterval } from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoLike: false,
    videoData:{},
    commentSize: 4,
    curCommentPage: 1,
    commentList: [],
    catName: '',
    comment: '',
    replyCount: 2
  },
  /**
   * 生命周期函数--监听页面加载
   */
  getData: function(vid, isCloseLoing){
    
    getVideoDetail(vid, (data) => {
      data.pubTime = tsFormatTime(data.pubtime, "Y-M-D h:m");
      var vLike = wx.getStorageSync('videoLike');
      this.setData({
        videoData: data,
        videoLike: vLike[data.id] ? vLike[data.id] : "false"
      });
      getCommentList(this.data.videoData.id, this.data.commentSize, this.data.curCommentPage, (data) => {
        var cLike = wx.getStorageSync('commentLike');
        data.forEach((item) => {
          item.interval = retTimeInterval(item.pubtime);
          item.liked = cLike[item.cid] ? cLike[item.cid] : false;
        });
        console.log(data)
        this.setData({
          commentList: data
        })
        if (isCloseLoing){
          // 隐藏导航栏加载框
          wx.hideNavigationBarLoading();
          // 停止下拉动作
          wx.stopPullDownRefresh();
        }
      });
    });
  },
  onLoad: function (options) {
    console.log(options);
    this.setData({
      catName: options.catName
    })
    this.getData(options.id);
    var sid = options.id;
    try {
      var value = wx.getStorageSync('spid');
      if (value.length != 0) {
        value.push(sid);
        let array = Array.from(new Set(value));
        console.log(array);
        wx.setStorage({
          key: 'spid',
          data: array
        })
      }else{
        var value = [];
        value.push(sid);
        wx.setStorage({
          key: 'spid',
          data: value
        })
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  videoStart: function(e){
    VideoWatch(this.data.videoData.id)
  },
  inputEvent: function(e){
    // console.log(e);
    this.setData({
      comment: e.detail.value
    })
  },
  toCommentPage: function(e){
    wx.navigateTo({
      url: '/pages/comment/comment?cid='+ e.currentTarget.dataset.topicid
    })
  },
  formSubmit: function(e){
    // 检查登录状态
  
    wx.getSetting({
      success: function(res) {
        console.log('哦哦哦我in');
        if(res.authSetting['scope.userInfo']){
          var reg = /^\s*\s+$/g;
          var input = this.data.comment;
          // console.log(this.data);
          if (input.replace(reg, '') == "") {
            wx.showModal({
              title: '提示',
              content: '输入内容不可以为空哦',
              showCancel: false
            })
          } else {
            var vd = this.data.videoData;
            var openid = wx.getStorageSync('openid');
            addComment(vd.id, 0, openid, '', input, (data) => {
              if (data.errmsg === "success" && data.errcode === 0) {
                getCommentList(this.data.videoData.id, this.data.commentSize, this.data.curCommentPage, (data) => {
                  var cLike = wx.getStorageSync('commentLike');
                  data.forEach((item) => {
                    item.interval = retTimeInterval(item.pubtime);
                    item.liked = cLike[item.cid];
                  });
                  this.setData({
                    commentList: data
                  })
                  console.log(this.data);
                })
                this.setData({
                  comment: ""
                })
              } else {
                console.log("发表评论失败", data);
              }
            })
          }
        }else{
          wx.showModal({
            title: '授权提示',
            content: '对不起, 要使用评论功能需要授权公开您的信息, 您可以点击主页左上角的头像,引导授权',
            confirmText: "返回主页",
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/video/video',
                });
              }
            }
          })
        }
      }.bind(this)
    }) 
  },
  // 监听用户下拉动作
  onPullDownRefresh: function(){
    wx.showNavigationBarLoading();
    this.getData(this.data.videoData.id, true)
  }
})